#include "hal_function.h"
#include "ssd1306.h"
#include "lvgl.h"
#include "cynfsm.h"
#include "oled_mgr.h"
#include "ui.h"

DEF_CYN_FSM_OBJ_STATIC(oled,OLED_PAGE_MAX)

static QueueHandle_t messageQueue = 0;
oledMessage oledMsg;
OLED_PubData_Handle oledPubData;

int oledMessage_Button(uint8_t button)
{
    oledMessage msg = {};
    msg.source_page = OLED_PAGE_MAX;
    msg.data.max.datatype = OLED_EX_DATATYPE_BUTTON;
    msg.data.max.data.button.button = button;
    return oledSendMessage(&msg);
}


int oledSendMessage(oledMessage *m)
{
   
   if(messageQueue)
   {
       if(pdPASS ==xQueueSend(messageQueue, ( void * ) m, ( TickType_t ) 0 ))
       {
          return 0x0;
       }
   }
   return 0xff;
}

void showOLEDInit()
{
    messageQueue = xQueueCreate(OLED_QUEUE_SIZE, sizeof(oledMessage));
    cynFsmObjInit(CYN_FSM_OBJ(oled),OLED_PAGE_MAX);
    cynFsmRegister(CYN_FSM_OBJ(oled),OLED_PAGE_Start,startPage_Entry,startPage_Process,startPage_Exit);
    cynFsmRegister(CYN_FSM_OBJ(oled),OLED_PAGE_HOME,showHomePage_Entry,showHomePage_Process,showHomePage_Exit);
}







static void msgDeal(uint32_t arg)
{
    
    if( xQueueReceive( messageQueue,&oledMsg,( TickType_t ) 5 ) == pdPASS )
    {
        switch(oledMsg.source_page)
        {
            case OLED_PAGE_Start:
                if(oledMsg.data.start.start_finish)
                {
                    cynFsmJump(CYN_FSM_OBJ(oled),OLED_PAGE_HOME,0);
                }
            break;

            case OLED_PAGE_HOME:

            break;
            default:
            break;
        }
    }
}



uint32_t showOLED_Process(uint32_t arg)
{
    static uint8_t flag = 0;

    if(flag == 0)
    {
        showOLEDInit();
        //go to start page
        cynFsmJump(CYN_FSM_OBJ(oled),OLED_PAGE_Start,0);
        flag = 1;
    }
    else
    {
        msgDeal(arg);
        cynFsmObjSetArg(CYN_FSM_OBJ(oled),(uint32_t)(&oledMsg));
        cynFsmRun(CYN_FSM_OBJ(oled));
        cynFsmObjGetRes(CYN_FSM_OBJ(oled));
    }
}


